package guowei.guliproduct.product.service.impl;

import guowei.guliproduct.product.entity.CategoryEntity;
import guowei.guliproduct.product.service.BrandService;
import guowei.guliproduct.product.service.CategoryService;
import guowei.guliproduct.product.vo.BrandsRelationVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import guowei.common.utils.PageUtils;
import guowei.common.utils.Query;

import guowei.guliproduct.product.dao.CategoryBrandRelationDao;
import guowei.guliproduct.product.entity.CategoryBrandRelationEntity;
import guowei.guliproduct.product.service.CategoryBrandRelationService;
import org.springframework.transaction.annotation.Transactional;


@Service("categoryBrandRelationService")
public class CategoryBrandRelationServiceImpl extends ServiceImpl<CategoryBrandRelationDao, CategoryBrandRelationEntity> implements CategoryBrandRelationService {

    @Autowired
    private CategoryService categoryService;

    @Autowired
    private BrandService brandService;

    @Autowired
    private CategoryBrandRelationService categoryBrandRelationService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<CategoryBrandRelationEntity> page = this.page(
                new Query<CategoryBrandRelationEntity>().getPage(params),
                new QueryWrapper<CategoryBrandRelationEntity>()
        );

        return new PageUtils(page);
    }

    /**
     * 根据brandId查询关联关系
     * @param params
     * @return
     */
    @Override
    public PageUtils queryPagrByBrandId(Map<String, Object> params) {
        QueryWrapper<CategoryBrandRelationEntity> wrapper = new QueryWrapper<>();
        Object brandId = params.get("brandId");
        wrapper.eq("brand_id",brandId);
        IPage<CategoryBrandRelationEntity> page = this.page(new Query<CategoryBrandRelationEntity>().getPage(params),
                wrapper);
        return new PageUtils(page);
    }

    /**
     * 根据前端传来的catelog_id得到catelogname并显示在页面上，还需要在brand和category里配置更新修改字段
     *
     * Transactional是spring中常用的注解之一，通常情况下我们在需要对一个service方法添加事务时，
     * 加上这个注解，如果发生unchecked exception，就会发生rollback
     * @param categoryBrandRelation
     */
    @Transactional
    @Override
    public void saveCateLogIdBrandId(CategoryBrandRelationEntity categoryBrandRelation) {
        CategoryEntity category = categoryService.getById(categoryBrandRelation.getCatelogId());
        String catelogname = category.getName();
        String brandName = brandService.getById(categoryBrandRelation.getBrandId()).getName();
        categoryBrandRelation.setCatelogName(catelogname);
        categoryBrandRelation.setBrandName(brandName);
        categoryBrandRelationService.save(categoryBrandRelation);
    }

    @Override
    public ArrayList<BrandsRelationVo> selectallRelations(Long catId) {
        List<CategoryBrandRelationEntity> list = this.list(new QueryWrapper<CategoryBrandRelationEntity>().eq("catelog_id", catId));
        List<BrandsRelationVo> collect = list.stream().map(categoryBrandRelationEntity -> {
            BrandsRelationVo brandsRelationVo = new BrandsRelationVo();
            brandsRelationVo.setBrandId(categoryBrandRelationEntity.getBrandId());
            brandsRelationVo.setBrandName(categoryBrandRelationEntity.getBrandName());
            return brandsRelationVo;
        }).collect(Collectors.toList());
        return (ArrayList<BrandsRelationVo>) collect;

    }


}